#coding=utf-8
from uliweb import expose

@expose('/group/<group_name>/document')
def group_document(group_name):
    from models import find_group
    from uliweb.orm import get_model
    
    Document = get_model('document')
    group = find_group(group_name)
    documents = group.documents.all().order_by(Document.c.create_time.desc())
    return {'group':group, 'documents':documents}

@expose('/group/<group_name>/document/new_category')
def group_new_category(group_name):
    from models import find_group
    from document.forms import NewCategoryForm
    from document.models import new_category
    
    group = find_group(group_name)
    form = NewCategoryForm(data={'id':group.id})
    if request.method == 'GET':
        return {'form':form, 'group':group}
    else:
        flag = form.validate(request.params)
        if flag:
            c = new_category(form.name.data)
            group.categories.add(c)
            #todo creating real category path
            flash('New category added successfully!') 
            return redirect('/group/%s/document' % group.name)
        else:
            return {'form':form, 'group':group}
        
@expose('/group/<group_name>/document/category/<int:category_id>')
def group_category(group_name, category_id):
    from models import find_group
    from uliweb.orm import get_model
    
    Document = get_model('document')
    Category = get_model('category')
    group = find_group(group_name)
    category = Category.get(int(category_id))
    documents = category.category_documents.all().order_by(Document.c.create_time.desc())
    return {'group':group, 'documents':documents, 'category':category}
    
@expose('/group/<group_name>/document/upload')
def group_upload(group_name):
    from models import find_group
    from document.forms import UploadForm
    from document.models import upload_document
    
    group = find_group(group_name)
    form = UploadForm(data={'id':group.id})
    categories = [('', ' ')]
    for c in group.categories.all():
        categories.append((str(c.id), c.name))
    UploadForm.category.choices = categories
    
    if request.method == 'GET':
        return {'form':form, 'group':group}
    else:
        flag = form.validate(request.POST, request.FILES)
        if flag:
            d = upload_document(form.filename, form.description.data, form.category.data, request.user, 'groups/%s' % group.name)
            group.documents_count += 1
            group.save()
            #add new document to group
            group.documents.add(d)
            flash('Document uploaded successfully!') 
            return redirect('/group/%s/document' % group.name)
        else:
            return {'form':form, 'group':group}
    
@expose('/group/<group_name>/document/<int:document_id>/delete')
def group_del_document(group_name, document_id):
    from models import find_group
    from document.models import delete_document

    group = find_group(group_name)
    _id = delete_document(document_id)
    group.documents_count -= 1
    group.save()
    
    group.documents.delete(_id)
    return redirect('/group/%s/document' % group.name)
    
@expose('/group/<group_name>/document/<int:document_id>')
def group_view_document(group_name, document_id):
    from document.models import Document
    from models import find_group
    
    group = find_group(group_name)
    doc = Document.get(int(document_id))
    return {'doc':doc, 'group':group}

@expose('/group/<group_name>/document/<int:document_id>/edit')
def group_edit_document(group_name, document_id):
    from document.forms import EditForm
    from models import find_group
    from document.models import Document, edit_document
    
    group = find_group(group_name)
    doc = Document.get(int(document_id))
    categories = [('', ' ')]
    for c in group.categories.all():
        categories.append((str(c.id), c.name))
    EditForm.category.choices = categories
    form = EditForm(data={'id':group.id, 'description':doc.description, 'category':str(doc.category.id)})
    
    if request.method == 'GET':
        return {'form':form, 'group':group, 'doc':doc}
    else:
        flag = form.validate(request.POST)
        if flag:
            f = edit_document(doc, form.description.data, form.category.data, request.user, 'groups/%s' % group.name)
            if f:
                flash('Document changed successfully!')
            else:
                flash("You've not changed the document.")
            return redirect('/group/%s/document' % group.name)
        else:
            return {'form':form, 'group':group}
    